package dataStructure.LinkedList;

/**
 * @author lhn
 * @date 2024-04-16 20:51
 */

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        System.out.println("This is test");
        HeroNode2 hero1 = new HeroNode2(1,"宋江","及时雨");
        HeroNode2 hero2= new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3,"吴用","智多星");
        HeroNode2 hero4 = new HeroNode2(4,"林冲","豹子头");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.list();

        System.out.println("This is update");
        HeroNode2 hero5 = new HeroNode2(4,"公孙胜","入云龙");
        doubleLinkedList.update(hero5);
        doubleLinkedList.list();

        System.out.println("This is delete");
        doubleLinkedList.delete(4);
        doubleLinkedList.list();

        System.out.println("This is addByOrder");
        doubleLinkedList.addByOrder(hero5);
        doubleLinkedList.list();
    }
}
class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    public void setHead(HeroNode2 head) {
        this.head = head;
    }
    //添加到双向链表末尾
    public void add(HeroNode2 heroNode) {
        //因为head节点不能动，因此我们需要一个辅助遍历temp
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //当退出while循环时，temp就指向了链表的最后
        //将最后这个节点的next指向新的节点
        temp.next = heroNode;
        heroNode.prev = temp;
    }
    public void addByOrder(HeroNode2 heroNode) {
        //因为单链表，temp是位于添加位置的前一个节点
        HeroNode2 temp = head;
        boolean flag = false;   //flag标志添加的编号是否存在，默认为false
        while (true) {
            if (temp.next == null ){
                //已经到链表的最后
                break;
            }
            if (temp.next.no > heroNode.no) {
                //在temp的d当前位置添加
                break;
            } else if (temp.next.no == heroNode.no) {
                //编号已存在
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("准备插入的英雄编号 %d 已经存在",heroNode.no);
        } else {
            temp.next = heroNode;
            heroNode.prev = temp;
        }
    }
    //修改节点的信息，根据no编号来修改，no编号不能修改
    public void update(HeroNode2 newHeroNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while(true) {
            if (temp == null) {
                System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no);
                break;
            }
            if (newHeroNode.no == temp.no) {
                temp.name = newHeroNode.name;
                temp.nickname = newHeroNode.nickname;
                System.out.println("修改成功");
                break;
            }
            temp = temp.next;
        }
    }
    public void delete(int no ) {
        HeroNode2 temp = head.next;
        while(true) {
            if (temp == null) {
                System.out.printf("没有找到编号%d的节点,不能删除\n",no);
                break;
            }
            if (temp.no == no) {
                temp.prev.next = temp.next;
                //如果是最后一个节点，就不需要执行下面
                if (temp.next!=null) {
                    temp.next.prev = temp.prev;
                }
                System.out.println("删除成功");
                break;
            }
            temp = temp.next;
        }
    }
    //遍历双向链表
    //toString方法不能prev和next
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为头节点不能动，需要辅助变量
        HeroNode2 temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null ){
                break;
            }
            System.out.println(temp);
            //将next后移
            temp = temp.next;
        }
    }
}
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 prev;
    public HeroNode2 next;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
